package com.lisn.springfileuploaddownloadserver;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

@Controller
public class FileController {
    @Value("${upload.file.path}")  // 读取资源配置参数
    private String uploadPathStr;

    public FileController() {
    }

    @PostMapping("/upload")
    public @ResponseBody
    boolean upload(@RequestParam MultipartFile file, @RequestParam String filename) {
        System.out.println("----------------1---------------");
        if (file == null || file.isEmpty() || filename == null || filename.isEmpty()) {
            System.out.println("----------------文件或文件名不能为空---------------");
            return false;
        }
        try (InputStream inputStream = file.getInputStream()) {
            Path uploadPath = Paths.get(uploadPathStr);
            System.out.println("uploadPathStr = " + uploadPathStr);
            if (!uploadPath.toFile().exists())
                uploadPath.toFile().mkdirs();
            // 第一个InputStream为上传文件的输入流，第二个Path为存储文件的路径，resolve(filename)相当于在上传目录下的filename文件
            Files.copy(inputStream, Paths.get(uploadPathStr).resolve(filename), StandardCopyOption.REPLACE_EXISTING);
            System.out.println("upload file , filename is " + filename);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("-------2 " + e.toString());
            return false;
        }
    }

    @PostMapping("/download")
    public ResponseEntity<FileSystemResource> download(@RequestParam String filename) {
        if (filename == null || filename.isEmpty()) {
            System.out.println("filename 不能为空 ---------");
            return null;
        }
        File file = Paths.get(uploadPathStr).resolve(filename).toFile();
        if (file.exists() && file.canRead()) {
            System.out.println("download file , filename is " + filename);
            return ResponseEntity.ok()
                    .contentType(file.getName().contains(".jpg") ? MediaType.IMAGE_JPEG : MediaType.IMAGE_PNG)
                    .body(new FileSystemResource(file));
        } else {
            System.out.println("目标文件不存在---------");
            return null;
        }
    }
}
